package com.weayer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weayer.constant.CommonStatusEnum;
import com.weayer.dto.PriceRule;
import com.weayer.dto.ResponseResult;
import com.weayer.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PriceRuleService{
    @Autowired
    PriceRuleMapper priceRuleMapper;

    public ResponseResult add(PriceRule priceRule){
        // 1. 拼接 fareType
        String citeCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        priceRule.setFareType(citeCode + "$" + vehicleType);
        // 2. 获取历史版本规则，若重复则不添加
//        Map<String, Object> map = new HashMap<>();
//        map.put("city_code", citeCode);
//        map.put("vehicle_type", vehicleType);
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", citeCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");
        List<PriceRule> priceRules =  priceRuleMapper.selectList(queryWrapper);
        int fareVersion = 0;
        if(priceRules.size() > 0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(), CommonStatusEnum.PRICE_RULE_EXISTS.getValue());
        }
        priceRule.setFareVersion(++fareVersion);
        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    public ResponseResult edit(PriceRule priceRule){
        // 1. 拼接 fareType
        String citeCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();
        priceRule.setFareType(citeCode + "$" + vehicleType);
        // 2. 获取历史版本规则，若重复则不修改
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", citeCode);
        queryWrapper.eq("vehicle_type", vehicleType);
        queryWrapper.orderByDesc("fare_version");
        List<PriceRule> priceRules =  priceRuleMapper.selectList(queryWrapper);
        int fareVersion = 0;
        if(priceRules.size() > 0){
            PriceRule lastPriceRule = priceRules.get(0);
            Double unitPricePerMile = lastPriceRule.getUnitPricePerMile();
            Double unitPricePerMinute = lastPriceRule.getUnitPricePerMinute();
            Double startFare = lastPriceRule.getStartFare();
            Integer startMile = lastPriceRule.getStartMile();

            if (unitPricePerMile.doubleValue() == priceRule.getUnitPricePerMile().doubleValue()
                    && unitPricePerMinute.doubleValue() == priceRule.getUnitPricePerMinute().doubleValue()
                    && startFare.doubleValue() == priceRule.getStartFare().doubleValue()
                    && startMile.intValue() == priceRule.getStartMile().intValue()){
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_EXISTS.getValue());
            }

            fareVersion = priceRules.get(0).getFareVersion();
        }
        priceRule.setFareVersion(++fareVersion);
        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }
    /**
     * @Description: 查询最新计价规则
     * @Param: [fareType]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult getNewest(String fareType){
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fare_type", fareType);
        queryWrapper.orderByDesc("fare_version");
        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        if(priceRules.size() > 0){
            return ResponseResult.success(priceRules.get(0));
        }else{
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
    }
    /**
     * @Description:  判断当前规则是否是最新规则
     * @Param: [fareType, fareVersion]
     * @return: com.weayer.dto.ResponseResult<com.weayer.dto.PriceRule>
     **/
    public ResponseResult<Boolean> isNewest(String fareType, Integer fareVersion){
        ResponseResult<PriceRule> responseResult = getNewest(fareType);
        if(responseResult.getCode() == CommonStatusEnum.PRICE_RULE_EMPTY.getCode()){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
        if(responseResult.getData().getFareVersion() == fareVersion){
            return ResponseResult.success(true);
        }else{
            return ResponseResult.fail(false);
        }
    }
    /**
     * @Description: 根据城市代码、车辆类型，查询计价规则是否存在
     * @Param: [cityCode, vehicleType]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult ifExists(PriceRule priceRule){
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code", priceRule.getCityCode());
        queryWrapper.eq("vehicle_type", priceRule.getVehicleType());
        queryWrapper.orderByDesc("fare_version");
        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        if(priceRules.size() > 0){
            return ResponseResult.success(true);
        }else{
            return ResponseResult.success(false);
        }
    }
}
